package com.hybrid.webview;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-10 22:03
 * @version: 9.1.0
 */
public class ParamBuilder {
    private JSONObject mParamJSONObject = new JSONObject();

    public ParamBuilder() {

    }

    public ParamBuilder(JSONObject param) {
        initParam(param);
    }

    public ParamBuilder initParam(JSONObject param) {
        if (null != param) {
            mParamJSONObject = param;
        }
        return this;
    }

    public ParamBuilder addParamString(String key, String value) {
        if (null != key && null != value) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamBoolean(String key, boolean value) {
        if (null != key) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamDouble(String key, double value) {
        if (null != key) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamInt(String key, int value) {
        if (null != key) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamLong(String key, long value) {
        if (null != key) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamJsonObject(String key, JSONObject value) {
        if (null != key && null != value) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public ParamBuilder addParamJsonArray(String key, JSONArray value) {
        if (null != key && null != value) {
            try {
                mParamJSONObject.put(key, value);
            } catch (JSONException e) {
            }
        }
        return this;
    }

    public String getParamString(String key, String defaultValue) {
        String result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getString(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public boolean getParamBoolean(String key, boolean defaultValue) {
        boolean result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getBoolean(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public double getParamDouble(String key, double defaultValue) {
        double result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getDouble(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public int getParamInt(String key, int defaultValue) {
        int result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getInt(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public long getParamLong(String key, long defaultValue) {
        long result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getLong(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public JSONObject getParamJsonObject(String key, JSONObject defaultValue) {
        JSONObject result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getJSONObject(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public JSONArray getParamJsonArray(String key, JSONArray defaultValue) {
        JSONArray result = defaultValue;
        try {
            if (null != key) {
                result = mParamJSONObject.getJSONArray(key);
            }
        } catch (JSONException e) {
        }
        return result;
    }

    public JSONObject buildParamJSONObject() {
        return mParamJSONObject;
    }

    public String buildParamString() {
        return mParamJSONObject.toString();
    }
}
